/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.adama.validator;

import fr.adama.dto.BulletinPaieDTO;
import fr.adama.dto.LigneFactureModeleDTO;
import fr.adama.dto.ModeleFactureDTO;
import fr.adama.dto.ModelePaieDTO;
import java.util.Locale;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;

/**
 *
 * @author mini-john
 */
public class ModeleFactureValidator implements Validator {

    private static final Logger logger = LoggerFactory.getLogger(ModelPaieValidator.class);
    MessageSource messageSource;

    public ModeleFactureValidator(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    @Override
    public boolean supports(Class<?> type) {
        return ModeleFactureDTO.class.equals(type);
    }

    @Override
    public void validate(Object o, Errors errors) {
         ModeleFactureDTO factureModele = (ModeleFactureDTO) o;
        //ValidationUtils.rejectIfEmpty(errors, "nom", "Size.modelPaie.nom");
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

        javax.validation.Validator validator = factory.getValidator();
        BindingResult bindingResult = (BindingResult) errors;
        Set<ConstraintViolation<ModeleFactureDTO>> constraintViolationFacture
                = validator.validate(factureModele);
        for (ConstraintViolation<ModeleFactureDTO> contraintes : constraintViolationFacture) {
            logger.error(contraintes.getRootBeanClass().getSimpleName()
                    + "." + contraintes.getPropertyPath() + " " + contraintes.getMessage());

            ObjectError error = new ObjectError(contraintes.getPropertyPath().toString(), messageSource.getMessage(contraintes.getMessage().replace("{", "").replace("}", ""), new String[]{}, new Locale("fr")));
            bindingResult.addError(error);

        }
        for (LigneFactureModeleDTO ligne : factureModele.getLigneFactureModeles()) {

            Set<ConstraintViolation<LigneFactureModeleDTO>> constraintViolationLigne
                    = validator.validate(ligne);
            for (ConstraintViolation<LigneFactureModeleDTO> contraintes : constraintViolationLigne) {
                logger.error(contraintes.getRootBeanClass().getSimpleName()
                        + "." + contraintes.getPropertyPath() + " " + contraintes.getMessage());

                ObjectError error = new ObjectError(contraintes.getPropertyPath().toString(), messageSource.getMessage(contraintes.getMessage().replace("{", "").replace("}", ""), new String[]{}, new Locale("fr")));
                bindingResult.addError(error);

            }
        }

    }
}



